<template>
  <div class="m-25 mt-65">
    <div class="normal-box-mod">
      <div class="normal-box-hd clearfix">
        <div class="normal-box-title">
          <div class="decorate-square"></div>
          <h2>清运车投递记录</h2>
        </div>
      </div>
      <div class="normal-box-bd">
        <Card style="margin-bottom: 10px">
          <div class="normal-list-page-hd clearfix">
            <Select class="mr-10" v-model="queryData.garbageType"
                    style="width: 200px">
              <Option v-for="item in typeList"
                      :value="item.label"
                      :key="item.label">{{ item.label }}</Option>
            </Select>
            <Button type="primary"
                    custom-icon="iconfont icon-chaxun"
                    size="small"
                    @click="getTableData">查询</Button>
          </div>
        </Card>
        <div class="normal-list-page-mod">
          <!-- <div class="mb-2">
                    <Button-group>
                      <Button custom-icon="iconfont icon-shanchusekuai" size="small" type="error" @click="batchDel">
                        删除
                      </Button>
                      <Button custom-icon="iconfont icon-fangkuai2" size="small" type="primary" @click="showCreateModal">
                        新建
                      </Button>
                    </Button-group>
                  </div> -->
          <div class="normal-list-page-bd">
            <Table border
                   :loading="list.loading"
                   :columns="list.columns"
                   :data="list.tableData"
                   @on-selection-change="selectionChange"></Table>
          </div>
          <div class="normal-list-page-ft clearfix">
            <Page class="fr"
                  :total="queryData.totalRecord"
                  :page-size="queryData.pageSize"
                  :current="queryData.page"
                  @on-change="changePage"
                  @on-page-size-change="onPageSizeChange"
                  show-elevator
                  show-total
                  show-sizer></Page>
          </div>
        </div>
        <Modal v-model="modal.mallAdvertising.bigPicVisible"
               width="1000px">
          <img :src="modal.mallAdvertising.bigPicUrl"
               style=" width:100%;height:100%" />
          <div slot="footer" />
        </Modal>
        <!--新建&修改-->
        <Modal v-model="modal.mallAdvertising.isShow"
               :loading="modal.mallAdvertising.loading"
               :mask-closable="false"
               :title="modal.mallAdvertising.title"
               @on-ok="postData">
          <Form ref="systemFrom"
                :model="modal.mallAdvertising.formData"
                :rules="modal.mallAdvertising.rules"
                :label-width="120">
            <FormItem label="广告标题"
                      prop="name">
              <Input placeholder="请输入广告标题"
                     maxlength="100"
                     v-model="modal.mallAdvertising.formData.name"
                     style="width:300px;" />
            </FormItem>
            <FormItem label="上传图片"
                      prop="url">
              <upload-image v-if="modal.mallAdvertising.isShow"
                            ref="upload"
                            :uploadList.sync="modal.mallAdvertising.formData.uploadList"
                            @imageupload="calcImg"
                            imgSize="1"></upload-image>
            </FormItem>
            <FormItem label="广告类型"
                      prop="status">
              <Select v-model="modal.mallAdvertising.formData.position"
                      class="mr-10"
                      :clearable=flog
                      style="width:100px">
                <Option v-for="item in modal.positionList"
                        :value="item.id"
                        :key="item.id">{{item.name}}</Option>
              </Select>
            </FormItem>
            <FormItem label="活动内容"
                      prop="content">
              <Input type="textarea"
                     placeholder="请输入活动内容"
                     maxlength="100"
                     v-model="modal.mallAdvertising.formData.content"
                     style="width:300px;" />
            </FormItem>
            <FormItem label="广告开始时间"
                      prop="startTime">
              <DatePicker v-model="modal.mallAdvertising.formData.startTime"
                          @on-change="modal.mallAdvertising.formData.startTime=$event"
                          type="date"
                          format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择广告开始时间"
                          style="width: 200px"></DatePicker>
            </FormItem>
            <FormItem label="广告结束时间"
                      prop="endTime">
              <DatePicker v-model="modal.mallAdvertising.formData.endTime"
                          @on-change="modal.mallAdvertising.formData.endTime=$event"
                          type="date"
                          format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择广告结束时间"
                          style="width: 200px"></DatePicker>
            </FormItem>
            <FormItem label="是否启动"
                      prop="enabled">
              <i-switch :size="large"
                        v-model="modal.mallAdvertising.formData.enabled"
                        :value="modal.mallAdvertising.formData.enabled">
                <span slot="open">开</span>
                <span slot="close">关</span>
              </i-switch>
            </FormItem>
          </Form>
        </Modal>
      </div>
    </div>
  </div>
</template>
<script>
import deliverApi from '@/api/RecordsCenter/deliver.js'
import { Message } from 'iview'
import WangEditor from '@/components/Editor/WangEditor.vue'
import Uploadoss from '@/components/uploadoss'
import uploadImage from '@/components/upload-image/upload-image'
import global from '@/assets/js/global'
export default {
  name: 'mallAdvertising',
  components: {
    uploadImage,
    WangEditor,
    Uploadoss
  },
  data () {
    return {
      // 弹出层数据
      modal: {
        positionList: [
          { id: 1, name: '积分商城' },
          { id: 2, name: '新闻快讯' },
          { id: 3, name: '政策制度' },
          { id: 4, name: '分类知识' },
          { id: 5, name: '最新资讯' }
        ],
        mallAdvertising: {
          isShow: false,
          bigPicVisible: false,
          loading: true,
          bigPicUrl: '',
          type: 'create', // create 新建，update 修改
          title: '新建',
          rules: {
            name: [
              {
                required: true,
                message: '请填写广告标题',
                trigger: 'blur'
              }
            ],
            content: [
              {
                required: true,
                message: '请填写活动内容',
                trigger: 'blur'
              }
            ],
            startTime: [
              {
                required: true,
                message: '请选择开始时间',
                trigger: 'change',
                type: 'date'
              }
            ],
            endTime: [
              {
                required: true,
                message: '请选择结束时间',
                trigger: 'change',
                type: 'date'
              }
            ]
          },
          formData: {}
        }
      },
      // 列表
      list: {
        loading: false,
        selections: [],
        // 列表展示数据
        tableData: [],
        // 表格表头
        columns: [
        //   {
        //     type: 'selection',
        //     width: 60,
        //     align: 'center'
        //   },
          /** 开始自动生成 */
          { title: 'id', key: 'id', align: 'center', minWidth: 50 },
          {
            title: '用户卡号',
            key: 'userCard',
            align: 'center',
            minWidth: 130
          },
          {
            title: '用户名',
            key: 'userName',
            align: 'center',
            minWidth: 130
          },
          { title: '垃圾类型', key: 'garbageType', align: 'center', minWidth: 120 },
          {
            title: '垃圾照片',
            key: 'url',
            align: 'center',
            minWidth: 200,
            render: (h, params) => {
              return h('img', {
                attrs: {
                  src: params.row.garbageUrl
                },
                style: {
                  width: 'auto',
                  height: '100px'
                },
                on: {
                  click: () => {
                    this.showBigPic(params.row.garbageUrl) // 此处再用一个方法，建一个modal把图放大。
                  }
                }
              })
            }
          },
          {
            title: '清运时间',
            key: 'createTime',
            align: 'center',
            minWidth: 130
          },
          {
            title: '重量',
            key: 'weight',
            align: 'center',
            minWidth: 130
          },
          {
            title: '评价',
            key: 'score',
            align: 'center',
            minWidth: 130,
            render: (h, params) => {
              if (params.row.score === 1) {
                return h('div', '差')
              } else if (params.row.score === 2) {
                return h('div', '一般 ')
              } else if (params.row.score === 3) {
                return h('div', '优 ')
              } else {
                return h('div', '未评价')
              }
            }
          }
          /** 自动生成结束 */

        ]
      },
      typeList: [
        { label: '其它垃圾', value: '其它垃圾' },
        { label: '可回收垃圾', value: '可回收垃圾' },
        { label: '厨余垃圾', value: '厨余垃圾' },
        { label: '有毒有害', value: '有毒有害' }
      ],
      // 页面查询数据
      queryData: {
        limit: 10,
        page: 1,
        totalRecord: 0,
        garbageType: ''
      }
    }
  },
  methods: {
    showBigPic (item) {
      this.modal.mallAdvertising.bigPicVisible = true
      this.modal.mallAdvertising.bigPicUrl = item
    },
    // 获取列表
    getTableData () {
      const that = this
      deliverApi
        .getCarList(that.queryData)
        .then((res) => {
          console.log(res.data.list)
          that.list.tableData = res.data.list
          that.queryData.totalRecord = res.data.totalRecord
          that.queryData.page = res.data.pageNum
          that.queryData.limit = res.data.pageSize
        })
        .catch((error) => {
          Message.error(error)
        })
    },

    // 新建&修改
    postData () {
      const that = this
      that.$refs['systemFrom'].validate((valid) => {
        if (valid) {
          if (!that.modal.mallAdvertising.formData.id) {
            // 新建
            MallAdvertisingApi.add(that.modal.mallAdvertising.formData)
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          } else {
            // 修改
            that.modal.mallAdvertising.formData.startTime = global.time(
              that.modal.mallAdvertising.formData.startTime
            )
            that.modal.mallAdvertising.formData.endTime = global.time(
              that.modal.mallAdvertising.formData.endTime
            )
            MallAdvertisingApi.update(
              that.modal.mallAdvertising.formData.id,
              that.modal.mallAdvertising.formData
            )
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                console.log(that.modal.mallAdvertising.formData)
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          }
        } else {
          that.modal.mallAdvertising.loading = false
          that.$nextTick(() => {
            that.modal.mallAdvertising.loading = true
          })
        }
      })
    },

    // 删除
    delData (ids) {
      if (!ids.length) {
        return
      }
      this.$Modal.confirm({
        title: '确认删除',
        content:
          '<p>您确认删除id为 <strong>' +
          ids.toString() +
          '</strong> 的记录吗?</p><p>删除后将无法撤销，请谨慎操作！</p>',
        loading: true,
        onOk: () => {
          MallAdvertisingApi.del({
            ids: ids
          }).then((res) => {
            this.$Modal.remove()
            if (!res.code) {
              this.$Notice.success({
                title: '删除成功'
              })
              this.queryData.page = 1
              this.getTableData()
            } else {
              this.$Notice.error({
                title: res.message
              })
            }
          })
        }
      })
    },
    // 图片的校验
    calcImg () {
      let imgUrl = this.modal.mallAdvertising.formData.uploadList.map((v) => {
        if (v.length < 1) return v
        v = v.response.res.requestUrls[0]
        return v
      })
      this.modal.mallAdvertising.formData.url = imgUrl[0]
      console.log(this.modal.mallAdvertising.formData.url)
    },
    // 排量删除
    batchDel () {
      let ids = []
      this.list.selections.forEach((element) => {
        ids.push(element.id)
      })
      if (!ids.length) {
        this.$Message.error({
          content: '请先选择要删除的项！',
          duration: 3
        })
      }
      this.delData(ids)
    },

    // 全选，不全选
    selectionChange (args) {
      this.list.selections = args
    },

    // 每页显示记录数变更
    onPageSizeChange (pageSize) {
      this.queryData.limit = pageSize
      this.getTableData()
    },

    // 换页
    changePage (page) {
      this.queryData.page = page
      this.getTableData()
    },

    // 显示新建弹窗
    showCreateModal () {
      this.modal.mallAdvertising.type = 'create'
      this.modal.mallAdvertising.title = '新建'
      this.modal.mallAdvertising.formData = {}
      this.modal.mallAdvertising.isShow = true
      this.$refs.systemFrom.resetFields()
    },

    // 显示修改弹窗
    showUpdateModal (id) {
      this.$refs.systemFrom.resetFields()
      this.modal.mallAdvertising.type = 'update'
      this.modal.mallAdvertising.title = '修改'
      // 不请求后端，直接list取对象
      // this.modal.mallAdvertising.formData = JSON.parse(
      //   JSON.stringify(this.list.tableData)
      // )[index];
      const that = this
      MallAdvertisingApi.getDetail(id)
        .then((res) => {
          console.log(res.data.startTime)
          console.log(res.data.endTime)
          console.log(res.data)
          that.modal.mallAdvertising.formData = res.data
          let item = {
            status: 'finished',
            response: {
              res: {
                requestUrls: []
              }
            }
          }
          if (that.modal.mallAdvertising.formData.url) {
            item.response.res.requestUrls.push(
              that.modal.mallAdvertising.formData.url
            )
            that.modal.mallAdvertising.formData.uploadList = []
            that.modal.mallAdvertising.formData.uploadList.push(item)
          } else {
            that.modal.mallAdvertising.formData.uploadList = []
          }
        })
        .catch((error) => {
          Message.error(error)
        })
      this.modal.mallAdvertising.isShow = true
    }
  },

  created () {
    this.getTableData()
  }
}
</script>
